home *** CD-ROM | disk | FTP | other *** search
/ Windows 95 API Bible / Windows 95 API Bible 3 Disc Set.iso / Win32 API Bible Book 2 of 3 / CHAPTER3 / GETBMP.C < prev    next >
C/C++ Source or Header  |  1996-03-06  |  9KB  |  265 lines

  1.  
  2. #include <windows.h>  
  3. #include <commctrl.h>
  4. #include "getbmp.h"  
  5.  
  6.  
  7.  
  8. #if defined (WIN32)
  9.     #define IS_WIN32 TRUE
  10. #else
  11.     #define IS_WIN32 FALSE
  12. #endif
  13.  
  14. #define IS_NT      IS_WIN32 && (BOOL)(GetVersion() < 0x80000000)
  15. #define IS_WIN32S  IS_WIN32 && (BOOL)(!(IS_NT) && (LOBYTE(LOWORD(GetVersion()))<4))
  16. #define IS_WIN95   (BOOL)(!(IS_NT) && !(IS_WIN32S)) && IS_WIN32
  17.  
  18. HINSTANCE hInst;   // current instance
  19.  
  20. LPCTSTR lpszAppName = "MyApp";
  21. LPCTSTR lpszTitle   = "TB_GETBITMAP"; 
  22.  
  23.  
  24. BOOL RegisterWin95( CONST WNDCLASS* lpwc );
  25.  
  26.  
  27. int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
  28.                       LPTSTR lpCmdLine, int nCmdShow)
  29. {
  30.    MSG      msg;
  31.    HWND     hWnd; 
  32.    WNDCLASS wc;
  33.  
  34.    wc.style         = CS_HREDRAW | CS_VREDRAW;
  35.    wc.lpfnWndProc   = (WNDPROC)WndProc;       
  36.    wc.cbClsExtra    = 0;                      
  37.    wc.cbWndExtra    = 0;                      
  38.    wc.hInstance     = hInstance;              
  39.    wc.hIcon         = LoadIcon (hInstance, lpszAppName); 
  40.    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  41.    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  42.    wc.lpszMenuName  = lpszAppName;              
  43.    wc.lpszClassName = lpszAppName;              
  44.  
  45.    if ( IS_WIN95 )
  46.    {
  47.       if ( !RegisterWin95( &wc ) )
  48.          return( FALSE );
  49.    }
  50.    else if ( !RegisterClass( &wc ) )
  51.       return( FALSE );
  52.  
  53.    hInst = hInstance; 
  54.  
  55.    hWnd = CreateWindow( lpszAppName, 
  56.                         lpszTitle,    
  57.                         WS_OVERLAPPEDWINDOW, 
  58.                         CW_USEDEFAULT, 0, 
  59.                         CW_USEDEFAULT, 0,  
  60.                         NULL,              
  61.                         NULL,              
  62.                         hInstance,         
  63.                         NULL               
  64.                       );
  65.  
  66.    if ( !hWnd ) 
  67.       return( FALSE );
  68.  
  69.    ShowWindow( hWnd, nCmdShow ); 
  70.    UpdateWindow( hWnd );         
  71.  
  72.    while( GetMessage( &msg, NULL, 0, 0) )   
  73.    {
  74.       TranslateMessage( &msg ); 
  75.       DispatchMessage( &msg );  
  76.    }
  77.  
  78.    return( msg.wParam ); 
  79. }
  80.  
  81.  
  82. BOOL RegisterWin95( CONST WNDCLASS* lpwc )
  83. {
  84.    WNDCLASSEX wcex;
  85.  
  86.    wcex.style         = lpwc->style;
  87.    wcex.lpfnWndProc   = lpwc->lpfnWndProc;
  88.    wcex.cbClsExtra    = lpwc->cbClsExtra;
  89.    wcex.cbWndExtra    = lpwc->cbWndExtra;
  90.    wcex.hInstance     = lpwc->hInstance;
  91.    wcex.hIcon         = lpwc->hIcon;
  92.    wcex.hCursor       = lpwc->hCursor;
  93.    wcex.hbrBackground = lpwc->hbrBackground;
  94.    wcex.lpszMenuName  = lpwc->lpszMenuName;
  95.    wcex.lpszClassName = lpwc->lpszClassName;
  96.  
  97.    // Added elements for Windows 95.
  98.    //...............................
  99.    wcex.cbSize = sizeof(WNDCLASSEX);
  100.    wcex.hIconSm = LoadImage(wcex.hInstance, lpwc->lpszClassName, 
  101.                             IMAGE_ICON, 16, 16,
  102.                             LR_DEFAULTCOLOR );
  103.             
  104.    return RegisterClassEx( &wcex );
  105. }
  106.  
  107. // Toolbar buttons. 
  108. //.................
  109. TBBUTTON tbButtons[] = 
  110.     { STD_FILENEW,  IDM_NEW, TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, 0L, 0}, 
  111.     { STD_FILEOPEN, IDM_OPEN, TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, 0L, 0}, 
  112.     { STD_FILESAVE, IDM_SAVE, TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, 0L, 0}, 
  113.     { 0, 0, TBSTATE_ENABLED, TBSTYLE_SEP, 0, 0L, 0}, 
  114.     { VIEW_LARGEICONS, IDM_LARGEICON, TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, 0L, 0}, 
  115.     { VIEW_SMALLICONS, IDM_SMALLICON, TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, 0L, 0}, 
  116.     { VIEW_LIST, IDM_LISTVIEW, TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, 0L, 0}, 
  117.     { VIEW_DETAILS, IDM_REPORTVIEW, TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, 0L, 0}, 
  118.     { 0, 0, TBSTATE_ENABLED, TBSTYLE_SEP, 0, 0L, 0},
  119.     { 0, IDM_TEST, TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, 0L, 0},
  120. }; 
  121.  
  122.  
  123. LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
  124. {
  125. static HWND hToolWnd   = NULL;
  126. static HWND hStatusWnd = NULL;
  127.  
  128.    switch( uMsg )
  129.    {
  130.       case WM_CREATE :
  131.               // Initialize the common control library
  132.               // and create the status window and toolbar.
  133.               //..........................................
  134.               InitCommonControls();
  135.  
  136.               hStatusWnd = CreateStatusWindow( 
  137.                                   WS_CHILD | WS_VISIBLE | 
  138.                                   CCS_BOTTOM | SBARS_SIZEGRIP, 
  139.                                   lpszTitle, hWnd, 101 );
  140.  
  141.               hToolWnd = CreateToolbarEx( hWnd, 
  142.                                           WS_CHILD | WS_BORDER | WS_VISIBLE, 
  143.                                           102, 
  144.                                           11,
  145.                                           (HINSTANCE)HINST_COMMCTRL, 
  146.                                           IDB_STD_SMALL_COLOR, 
  147.                                           tbButtons, 4,
  148.                                           0, 0, 100, 30, sizeof(TBBUTTON) );
  149.  
  150.               if ( hToolWnd )
  151.               {
  152.                  HBITMAP     hBmp;
  153.                  TBADDBITMAP tb;
  154.                  int index, stdidx;
  155.  
  156.                  // Add the system-defined view bitmaps.
  157.                  //.....................................
  158.                  tb.hInst = HINST_COMMCTRL;
  159.                  tb.nID = IDB_VIEW_SMALL_COLOR;
  160.                  stdidx = SendMessage(hToolWnd, TB_ADDBITMAP, 12, (LPARAM)&tb);
  161.  
  162.                  for (index = 4; index < 8; index++)
  163.                     tbButtons[index].iBitmap += stdidx;
  164.  
  165.                  SendMessage(hToolWnd, TB_ADDBUTTONS, 4, (LONG) &tbButtons[4]);
  166.  
  167.                  // Load bitmap to add to the toolbar.
  168.                  //...................................
  169.                  hBmp = CreateMappedBitmap( hInst, 100, 0, NULL, 0 );
  170.  
  171.                  tb.hInst = NULL;
  172.                  tb.nID   = (UINT) hBmp;
  173.                  stdidx = SendMessage(hToolWnd, TB_ADDBITMAP, 1, (LPARAM)&tb);
  174.  
  175.                  tbButtons[9].iBitmap += stdidx;
  176.  
  177.                  SendMessage(hToolWnd, TB_ADDBUTTONS, 2, (LONG) &tbButtons[8]);
  178.               }
  179.               break;
  180.  
  181.       case WM_SIZE :
  182.               {
  183.                  RECT clrect, strect;
  184.  
  185.                  // Size the status window when the main
  186.                  // window is sized.
  187.                  //.....................................
  188.                  GetClientRect( hWnd, &clrect );
  189.                  GetClientRect( hStatusWnd, &strect );
  190.  
  191.                  MoveWindow( hStatusWnd, 0, clrect.bottom-strect.bottom, 
  192.                              LOWORD( lParam ), HIWORD( lParam ), TRUE );
  193.               }
  194.               break;
  195.  
  196.       case WM_COMMAND :
  197.               switch( LOWORD( wParam ) )
  198.               {
  199.                  case IDM_NEW :
  200.                  case IDM_OPEN :
  201.                  case IDM_SAVE :
  202.                  case IDM_LARGEICON :
  203.                  case IDM_SMALLICON :
  204.                  case IDM_LISTVIEW :
  205.                  case IDM_REPORTVIEW :
  206.                         {
  207.                            // Get the bitmap of the selected button.
  208.                            //.......................................
  209.                            int nIdx = SendMessage(hToolWnd, TB_GETBITMAP, 
  210.                                                   LOWORD( wParam ), 0 );
  211.  
  212.                            // Change the IDM_TEST 
  213.                            // bitmap to the selected one.
  214.                            //............................
  215.                            SendMessage(hToolWnd, TB_CHANGEBITMAP, 
  216.                                        IDM_TEST, nIdx );  
  217.                         }
  218.                         break;
  219.  
  220.                  case IDM_ABOUT :
  221.                         DialogBox( hInst, "AboutBox", hWnd, (DLGPROC)About );
  222.                         break;
  223.  
  224.                  case IDM_EXIT :
  225.                         DestroyWindow( hWnd );
  226.                         break;
  227.               }
  228.               break;
  229.       
  230.       case WM_DESTROY :
  231.               PostQuitMessage(0);
  232.               break;
  233.  
  234.       default :
  235.             return( DefWindowProc( hWnd, uMsg, wParam, lParam ) );
  236.    }
  237.  
  238.    return( 0L );
  239. }
  240.  
  241.  
  242. LRESULT CALLBACK About( HWND hDlg,           
  243.                         UINT message,        
  244.                         WPARAM wParam,       
  245.                         LPARAM lParam)
  246. {
  247.    switch (message) 
  248.    {
  249.        case WM_INITDIALOG: 
  250.                return (TRUE);
  251.  
  252.        case WM_COMMAND:                              
  253.                if (   LOWORD(wParam) == IDOK         
  254.                    || LOWORD(wParam) == IDCANCEL)    
  255.                {
  256.                        EndDialog(hDlg, TRUE);        
  257.                        return (TRUE);
  258.                }
  259.                break;
  260.    }
  261.  
  262.    return (FALSE); 
  263. }
  264.